<template>
  <div id="app">
    <router-view v-if="isRouterAlive" />
  </div>
</template>

<script>
import { getSubscrMessage, checkLoginToken, queryLcCurrency } from '@/api/login'
import { queryBjfhList } from '@/api/financial/voucher'

export default {
  name: 'App',
  provide() {
    return {
      reload: this.reload
    }
  },
  data() {
    return {
      notifications: {},
      count: 0,
      isRouterAlive: true
    }
  },
  created() {
    this.dateNew()
    this.isLogin()
    this.bjfhList()
    this.currencyDetail()
  },
  methods: {
    // 查询公司默认本位币
    async currencyDetail() {
      await queryLcCurrency().then(res => {
        this.$store.dispatch('tagsView/setData', res.data)
      })
    },
    // 比较符号
    bjfhList() {
      queryBjfhList({}).then(res => {
        this.$store.dispatch('tagsView/bjfData', res.data.list)
      })
    },
    // 局部刷新
    reload() {
      this.isRouterAlive = false
      this.$nextTick(function() {
        this.isRouterAlive = true
      })
    },
    // 订阅消息提示
    showtime() {
      const _this = this
      const today = new Date()
      const minute = today.getMinutes()
      const fuserId = window.sessionStorage.getItem('fuserId')
      const hostName = location.hash
      // 登录页不发请求
      if (hostName === '#/login') {
        return
      }
      // 分钟数
      if (minute === 0 || this.count === 0) {
      // 登录或刷新执行一次
        if (fuserId) {
          this.count = 1
        } else {
          return
        }
        const data = {
          fuserId: fuserId
        }
        if (data.fuserId) {
          getSubscrMessage(data).then(res => {
            if (res.code === 0) {
              const h = this.$createElement
              this.closeAllNotification()
              res.data.messageList.forEach((item, index) => {
                setTimeout(() => {
                  const notify = this.$notify({
                    title: item.title,
                    dangerouslyUseHTMLString: true,
                    message: h('div',
                      [
                        h('p',
                          {
                            style: {
                              color: '#409eff',
                              cursor: 'pointer',
                              padding: '10px 0',
                              lineHeight: '22px',
                              letterSpacing: '0.8px',
                              textDecoration: 'underline'
                            }
                          },
                          item.content
                        )
                      ]
                    ),
                    onClick: function() {
                    // 点击跳转页面
                      if (item.isNeedRedirect === 1) {
                        _this.$router.push({ path: '' + item.url })
                      }
                      // 点击删除当前通知
                      _this.notifications[index].close()
                      delete _this.notifications[index]
                    },
                    offset: 70,
                    duration: 0
                  })
                  this.notifications[index] = notify
                }, index * 500)
              })
            }
          })
        }
      }
    },
    dateNew() {
      clearInterval()
      setInterval(this.showtime, 60000) // 设定函数自动执行时间为 1000 ms(1 s)
    },
    // 关闭所有通知
    closeAllNotification() {
      const _this = this
      for (const key in _this.notifications) {
        _this.notifications[key].close()
        delete _this.notifications[key]
      }
    },
    // 每30秒调用,用户只能在一个地方登录，不能在两个地方同时登陆
    isLogin() {
      clearInterval()
      setInterval(this.checkLogin, 30000) // 设定函数自动执行时间 1000 ms(1 s)
    },
    checkLogin() {
      const token = window.sessionStorage.getItem('loginToken')
      const fuserId = window.sessionStorage.getItem('fuserId')
      const DATA = {
        loginToken: token
      }
      if (!fuserId) {
        return
      }
      checkLoginToken(DATA).then(res => {
        if (res.code === -1) {
          sessionStorage.removeItem('fuserId')
          // sessionStorage.removeItem('loginToken')
          this.$store.dispatch('logout')
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            type: 'warning'
          })
        }
      })
    }
  }
}
</script>
<style lang="scss">
html, body {
 background-color: #E9ECF5;
}
.el-input .el-input__inner {
  height: 30px;
  line-height: 30px;
}
.el-input .el-input__prefix, .el-input__icon  {
  line-height: 26px;
}
.el-autocomplete-suggestion{
  position: fixed!important;
}
.el-table th.gutter {
  display: table-cell !important
}
.el-form-item__label {
  font-size: 12px;
  padding-right: 12px;
}
.el-dialog {
  margin-top: 5vh;
  .el-dialog__body {
    padding: 10px 20px;
  }
}
.el-table{
  .el-input-number .el-input__inner{
    padding: 0 5px;
    height: 25px;
    width: 100%;
  }
  .el-input-number .el-input{
    width: 100%;
  }
  .el-input-number--mini{
    width: 100%;
  }
  .el-input-number__decrease, .el-input-number__increase {
    display: none;
  }
  .el-table__footer-wrapper tbody td{
    background: #fdf6ec!important;
  }
}
</style>
